package com.flycat.common.util;

import com.flycat.dto.SupplierSettleDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yalei.chen
 * @Date 2017/9/29
 * @Since JDK 1.8
 */
public class TransferUtils {

    /**
     * po转换dto工具类
     * @param poList
     * @param dtoClazz
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T,V>List<V> poToDto(List<T> poList,Class<V> dtoClazz) {

        return poList.stream().map(s -> {

            V dto = null;
            try {
                dto = dtoClazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            BeanUtils.copyProperties(s, dto);
            return dto;

        }).collect(Collectors.toList());
    }

    /**
     * 分页po转换dto工具类
     * @param poList
     * @param dtoClazz
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T,V>PageInfo<V> pagePoToDto(Page<T> poList, Class<V> dtoClazz) {

        Page entities = (Page) poList.clone();
        entities.clear();
        List<V> dtoList = poToDto(poList, dtoClazz);
        entities.addAll(dtoList);

        return new PageInfo(entities);
    }


    /**
     * po转换dto工具类
     * @param po
     * @param dtoClazz
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T,V>V poToDto(T po,Class<V> dtoClazz) {

        V dto = null;
        try {
            dto = dtoClazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(po, dto);

        return dto;
    }

}
